package com.qf.medicaldoctoradmin.healthHead.collection.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.qf.common.entity.Articles;
import com.qf.common.entity.ArticlesCollect;
import com.qf.common.utils.DateUtils;
import com.qf.common.utils.RedisUtils;
import com.qf.common.utils.UserUtils;
import com.qf.medicaldoctoradmin.healthHead.article.dao.HealthHeadDao;
import com.qf.medicaldoctoradmin.healthHead.collection.dao.ArticleCollectDao;
import com.qf.medicaldoctoradmin.healthHead.collection.service.ArticleCollectService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ArticleCollectServiceImpl implements ArticleCollectService {

    @Resource
    private ArticleCollectDao articleCollectDao;
    @Resource
    private HealthHeadDao healthHeadDao;


    @Override
    public void collect(Integer articlesId) {
        // 收藏文章
        // 获取用户id
        Integer uid = UserUtils.getUid();
        // 如果没有收藏进行收藏，否则取消收藏
        String collectKey = String.format("collect:%s",articlesId);
        // 将当天收藏数存到redis中
        String todayCollect = String.format("collectNum:%s", DateUtils.formatDate(new Date(), "yyyyMMdd"));
        // 使用hashGet获取用户的id
        Integer v = (Integer) RedisUtils.hashGet(collectKey, uid.toString());
        if (v == null || v.equals(0)) {
            // 如果没有收藏，可以进行收藏
            RedisUtils.hashPut(collectKey, uid.toString(), 1);
            // 反复收藏的处理
            if (v != null) {
                articleCollectDao.update(null,
                        new LambdaUpdateWrapper<ArticlesCollect>()
                                .set(ArticlesCollect::getCollectFlag,1)
                                .eq(ArticlesCollect::getUserId,uid)
                                .eq(ArticlesCollect::getArticlesId,articlesId));
            } else {
                ArticlesCollect articlesCollect = new ArticlesCollect();
                articlesCollect.setArticlesId(articlesId);
                articlesCollect.setUserId(uid);
                articlesCollect.setCollectFlag(1);
                // 将收藏记录存到数据库里面
                articleCollectDao.insert(articlesCollect);

            }
            // 收藏数加1
            RedisUtils.hashIncr(todayCollect,articlesId.toString(),1);


        } else {
            // 已经收藏则取消收藏，将redis的值变为0
            RedisUtils.hashPut(collectKey,uid.toString(),0);
            // 数据库的状态设置为0
            articleCollectDao.update(null,
                    new LambdaUpdateWrapper<ArticlesCollect>()
                            .set(ArticlesCollect::getCollectFlag,0)
                            .eq(ArticlesCollect::getUserId,uid)
                            .eq(ArticlesCollect::getArticlesId,articlesId));
            RedisUtils.hashIncr(todayCollect,articlesId.toString(),-1);
        }
    }

    @Override
    public List<Articles> selectById(Integer userId) {
        List<ArticlesCollect> articlesCollect = articleCollectDao.selectList(new LambdaQueryWrapper<ArticlesCollect>()
                .eq(ArticlesCollect::getUserId, UserUtils.getUid()));
        List<Integer> collect = articlesCollect.stream()
                .map(ArticlesCollect::getArticlesId)
                .collect(Collectors.toList());
        List<Articles> articlesCollects = new ArrayList<>();
        if (!collect.isEmpty()) {
            articlesCollects = healthHeadDao.selectList(new LambdaQueryWrapper<Articles>()
                    .in(Articles::getId, collect));
        }
        return articlesCollects;
    }


}
